package com.qf.cosmetology.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.UUID;

/**
 * @Author: xiyang
 * @FileName: SmsUtil
 * @Date: Created in 2021/6/25 11:03
 * @Vserion:
 * @Description: TODO
 */
public class SmsUtil {

    public static void main(String[] args) {
        String code = SmsUtil.sendMsg("15892222603");
        System.out.println(code);
    }



    public static String sendMsg(String  phoneNumber) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tPsQq7pCRQ7ARUnGmQq", "DfgAzFCIEZoSVmA3U9usZHQ9ygLY44");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "阳阳商城");
        request.putQueryParameter("TemplateCode", "SMS_205409285");
        // 随机生成一个5位数的验证码
        String code = UUID.randomUUID().toString().substring(0, 5);
        HashMap<String, String> map = new HashMap<>();
        map.put("code",code);
        // 验证码的参数必须是json数据格式
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            //后续需要从response中的data数据中去判定code:ok
            String data = response.getData();
            // 将JSON格式字符串转换为JSONObject对象
            JSONObject jsonObject = JSONObject.parseObject(data);
            String code1 = jsonObject.getString("Code");
            if(code1.equalsIgnoreCase("OK")){
                return code;
            }
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }


}
